package com.gluck.cases.app.biz.impl.impl;

import com.github.pagehelper.PageInfo;
import com.gluck.cases.app.biz.impl.CasesGroupService;
import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.enums.CommonStatus;
import com.gluck.cases.common.utils.utils.AssertUtil;
import com.gluck.cases.common.utils.utils.JwtUtil;
import com.gluck.cases.common.utils.utils.ShiroUsersUtil;
import com.gluck.cases.core.block.CasesGroupBlock;
import com.gluck.cases.core.modal.constants.AuthConstants;
import com.gluck.cases.core.modal.dto.CasesGroupDTO;
import com.gluck.cases.core.modal.dto.CasesTestsDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName CasesGroupServiceImpl
 * @Desc CasesGroupService 实现类
 * @Author shiyongzhe
 * @Date 2022/6/3 - 4:29 下午
 **/
@Service
public class CasesGroupServiceImpl implements CasesGroupService {

    @Autowired
    private CasesGroupBlock casesGroupBlock;

    @Override
    public PageInfo<CasesGroupDTO> queryList(CasesGroupDTO dto, int page, int size) {
        PageInfo<CasesGroupDTO> dtoPageInfo = casesGroupBlock.queryGroupsByCreateBy(dto.getCreatedBy(), dto.getName(), page, size);
        return dtoPageInfo;
    }

    @Override
    public PageInfo<CasesTestsDTO> queryGroupDetailById(Long id, int page, int size) {
        PageInfo<CasesTestsDTO> casesTestsDTOPageInfo = casesGroupBlock.queryTestsByGroupId(id, page, size);
        return casesTestsDTOPageInfo;
    }

    @Override
    public boolean updateCasesApi(CasesGroupDTO dto) {
        boolean update = casesGroupBlock.updateGroup(dto);
        AssertUtil.isFalse(update, BizSerErrorEnum.CASES_GROUP_UPDATE_ERROR);
        return true;
    }

    @Override
    public boolean deleteCasesApis(List<Long> ids) {
        ids.stream().forEach(id -> {
            boolean delete = casesGroupBlock.deleteGroupById(id);
            AssertUtil.isFalse(delete, BizSerErrorEnum.CASES_GROUP_DELETE_ERROR);
        });
        return true;
    }

    @Override
    @Transactional
    public boolean associateCasesTests(Long gid, String name, List<Long> casesIds) {
        // step 1 获取当前操作人员信息
        String username = JwtUtil.getClaim(ShiroUsersUtil.getUserToken(), AuthConstants.USERNAME);
        // step 2 apis id 转拼接字符串
        String casesApiIds = StringUtils.join(casesIds.toArray(), ",");
        // step 3 遍历插入caststests
        casesGroupBlock.associateCasesTestsByGroupId(gid, username, casesIds);
        // step 4 插入用例分组
        CasesGroupDTO dto = new CasesGroupDTO();
        dto.setName(name);
        dto.setCasesIds(casesApiIds);
        dto.setCreatedBy(username);
        dto.setStatus(CommonStatus.STOP.getCode());
        AssertUtil.isFalse(casesGroupBlock.addGroup(dto), BizSerErrorEnum.CASES_GROUP_ADD_ERROR);
        return true;
    }
}
